package com.train.member.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.train.common.enums.impl.BizExceptionEnums;
import com.train.common.enums.impl.BusinessTypeEnums;
import com.train.common.enums.impl.StatusEnums;
import com.train.common.exception.GlobalException;
import com.train.common.utils.date.LocalDateTimeUtils;
import com.train.common.utils.id.SnowIdUtils;
import com.train.member.dto.SmsRecordDTO;
import com.train.member.mapper.SmsRecordMapper;
import com.train.member.po.SmsRecordPO;
import com.train.member.properties.CommonProperties;
import com.train.member.service.SmsRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author baitiaojun
* @description 针对表【sms_record】的数据库操作Service实现
* @createDate 2025-02-10 01:55:15
*/
@Service
@RequiredArgsConstructor
public class SmsRecordServiceImpl extends ServiceImpl<SmsRecordMapper, SmsRecordPO>
    implements SmsRecordService {

    private final SmsRecordMapper smsRecordMapper;

    private final CommonProperties commonProperties;

    /**
     * 保存短信记录
     * @param smsRecordDTO
     */
    @Override
    public void saveOne(SmsRecordDTO smsRecordDTO) {
        boolean isSaved = smsRecordMapper.saveOne(smsRecordDTO);
        if (!isSaved) {
            throw new GlobalException(BizExceptionEnums.EXP_SAVE_SMS_RECORD);
        }
    }

    /**
     * 查询短信记录数
     * @param smsRecordDTO
     * @return
     */
    @Override
    public Long querySmsRecord(SmsRecordDTO smsRecordDTO) {
        return smsRecordMapper.querySmsRecord(smsRecordDTO);
    }

    /**
     * 验证短信发送频率
     * @param mobile
     * @param smsCode
     */
    @Override
    public void validateSmsSendFrequency(String mobile, String smsCode) {
        LocalDateTime now = LocalDateTimeUtils.getNow(LocalDateTimeUtils.DATE_TIME_FORMAT);
        LocalDateTime before = now.minusMinutes(1);
        SmsRecordDTO smsRecordDTO = new SmsRecordDTO(mobile, now, before);
        Long recordNum = this.querySmsRecord(smsRecordDTO);
        if (recordNum > commonProperties.getSendSmsLimit()) {
            throw new GlobalException(BizExceptionEnums.EXP_FREQUENT_SEND_SMS);
        } else {
            smsRecordDTO.setBusinessType(String.valueOf(BusinessTypeEnums.SEND_SMS.getCode()));
            smsRecordDTO.setSendTime(now);
            smsRecordDTO.setSmsCode(smsCode);
            smsRecordDTO.setIsUsed(String.valueOf(StatusEnums.NO.getCode()));
            smsRecordDTO.setId(SnowIdUtils.getNextId());
            this.saveOne(smsRecordDTO);
        }
    }

    /**
     * 修改短信记录
     *
     * @param mobile
     * @param smsCode
     * @param status
     * @param useTime
     */
    @Override
    public void editSmsRecord(String mobile, String smsCode, String status, LocalDateTime useTime) {
        SmsRecordDTO smsRecordDTO = new SmsRecordDTO(mobile, smsCode, status, useTime);
        boolean isEdite = smsRecordMapper.editSmsRecord(smsRecordDTO);
        if (!isEdite) {
            throw new GlobalException(BizExceptionEnums.EXP_EDIT_SMS_RECORD);
        }
    }
}




